// map.js
import qqmap from '../../../utils/qqmap'
Page({
  data: {
    repairDetail: [],
    latitude: 28.2344771046278,
    longitude: 112.93007399734499,
    markers: [
      {
        id: 1,
        latitude: 28.2344771046278,
        longitude: 112.93007399734499,
        width: 24,
        height: 30,
      },
      {
        id: 2,
        latitude: 27.875277,
        longitude: 112.913237,
        iconPath: '../../../static/images/marker.png',
        width: 40,
        height: 40,
      },
    ],
  },
  async onLoad({ id }) {
    const { data, code } = await wx.http.get('/repair/' + id)
    if (code !== 10000) return wx.utils.toast('查询报修详情信息失败')
    // console.log(data)
    this.setData({
      repairDetail: data,
    })
    this.getroadmap()
  },
  // 展示路线
  getroadmap() {
    qqmap.direction({
      mode: 'driving', //可选值：'driving'（驾车）、'walking'（步行）、'bicycling'（骑行），不填默认：'driving',可不填
      //from参数不填默认当前地址
      from: '28.2344771046278,112.93007399734499',
      to: '27.875277,112.913237',
      success: (res) => {
        // console.log(res)
        var ret = res
        var coors = ret.result.routes[0].polyline
        const pl = []
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / 1000000
        }
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        this.setData({
          latitude: pl[0].latitude,
          longitude: pl[0].longitude,
          markers: [
            {
              id: 1,
              latitude: pl[0].latitude,
              longitude: pl[0].longitude,
              width: 24,
              height: 30,
            },
            {
              id: 2,
              latitude: pl[200].latitude,
              longitude: pl[200].longitude,
              iconPath: '../../../static/images/marker.png',
              width: 40,
              height: 40,
            },
          ],
          polyline: [
            {
              points: pl,
              color: '#16ad58',
              width: 4,
            },
          ],
        })
      },
      fail: function (error) {
        console.error(error)
      },
      complete: function (res) {
        console.log(res)
      },
    })
  },
  // 修改报修
  editRepair(ev) {
    console.log(ev.mark.id)
    wx.redirectTo({
      url: '../form/index?id=' + ev.mark.id,
    })
  },
  // 取消报修
  countermand(ev) {
    wx.http.put('/cancel/repaire/' + ev.mark.id)
    wx.redirectTo({
      url: '../list/index',
    })
  },
})
